<?php
/**
 * 客户跟进记录
 * @Author: wjf
 * @Date: 2022-12-01 09:14:08
 */


namespace app\crm\controller\client;

use app\crm\model\client\Ctrecord as ClientCtrecord;
use think\Controller;
use think\facade\Session;

class Ctrecord extends Controller
{
       /**
     * 主页面
     * @Author: wjf
     * @Date: 2022-12-01 09:16:16
     */    
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 遍历数据
     * @Author: wjf
     * @Date: 2022-12-03 10:54:15
     */    
    public function list()
    {
        if ($this->request->param('data')) {
            $aa = $this->request->get('data');
            $arr = [];
            foreach ($aa as $k => $v) {
                if (!$v) {
                    unset($aa[$k]);
                } else {
                    $k = 'crm_classify_follow.' . $k;
                    $arr[$k] = $v;
                }
            }
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = ClientCtrecord::withJoin('Ourclients')->where($arr)->limit($page, $_GET['limit'])->where('crm_classify_follow.classify',1)->select();
            $count = ClientCtrecord::withJoin('Ourclients')->where('classify_id', Session::get('f_id'))->where('crm_classify_follow.classify',1)->count();
            foreach ($data as $v) {
                $v['name'] = $v['Ourclients']['name'];
            }  
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        } else {
            $page = ($_GET['page'] - 1) * $_GET['limit'];
            $data = ClientCtrecord::withJoin('Ourclients')->limit($page, $_GET['limit'])->where('classify_id', Session::get('f_id'))->where('crm_classify_follow.classify',1)->select(); 
            $count = ClientCtrecord::withJoin('Ourclients')->where('classify_id', Session::get('f_id'))->where('crm_classify_follow.classify',1)->count();
            foreach ($data as $v) {
                $v['name'] = $v['Ourclients']['name'];
            }     
            return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
        }
    }

    /**
     * 全部客户跟进记录
     * @Author: wjf
     * @Date: 2022-12-06 11:37:40
     */    
    public function lists()
    {
        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $data = ClientCtrecord::withJoin('Ourclients')->limit($page, $_GET['limit'])->where('crm_classify_follow.classify',1)->select(); 
        $count = ClientCtrecord::withJoin('Ourclients')->where('crm_classify_follow.classify',1)->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }
    /**
     * 跟进 
     * @Author: wjf
     * @Date: 2022-12-01 11:38:24
     */
    public function followup()
    {
        if (!empty($_GET['id'])) {
            Session::set('f_id', $_GET['id']);
            $list = db('crm_client')->where('id', $_GET['id'])->select();
            $this->assign(['list' => $list]);
        }
        return $this->fetch();
    }

    /**
     * 跟进表添加数据
     * @Author: wjf
     * @Date: 2022-12-01 11:38:24
     */
    public function fpdata()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $data['classify_id'] = Session::get('f_id');
            $ClueFollow = new ClientCtrecord;
            if ($ClueFollow->save($data)) {
                return json(['code' => 1, 'msg' => '跟进成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟进失败']);
            }
        }
    }
}